package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dk.mapper.ProcedureSequenceMapper;
import com.dk.model.Procedure;
import com.dk.mapper.ProcedureMapper;
import com.dk.model.ProcedureSequence;
import com.dk.service.ProcedureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.vo.ProcedureSequenceVO;
import com.dk.vo.ProcedureVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-17
 */
@Service
public class ProcedureServiceImpl extends ServiceImpl<ProcedureMapper, Procedure> implements ProcedureService {
    @Resource
    private ProcedureMapper procedureMapper;
    @Resource
    private ProcedureSequenceMapper procedureSequenceMapper;

    @Override
    public List<ProcedureVO> getInfoList(ProcedureVO vo) {
         /*工序编码,工序名称,是否启用 模糊查询*/
        LambdaQueryWrapper<Procedure> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(vo.getProcedureId()),Procedure::getProcedureId,vo.getProcedureId());
        wrapper.like(StringUtils.isNotBlank(vo.getProcedureName()),Procedure::getProcedureName,vo.getProcedureName());
        wrapper.eq(StringUtils.isNotBlank(vo.getProcedureFlag()),Procedure::getProcedureFlag,vo.getProcedureFlag());
        List<ProcedureVO> collect = procedureMapper.selectList(null).stream().map(procedure -> {
            ProcedureVO procedureVO = new ProcedureVO();
            BeanUtils.copyProperties(procedure, procedureVO);
            return procedureVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void insertInfoProcedure(ProcedureVO procedureVO) {
        Procedure procedure =new Procedure();
        BeanUtils.copyProperties(procedureVO,procedure);
        procedureMapper.insert(procedure);
    }

    @Override
    public ProcedureVO getInfoProcedureById(String procedureId) {

        Procedure procedure = procedureMapper.selectById(procedureId);
        ProcedureVO procedureVO = new ProcedureVO();
        BeanUtils.copyProperties(procedure,procedureVO);
        //此时需要查询关联的顺序表去查询,set到集合中
        LambdaQueryWrapper<ProcedureSequence> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcedureSequence::getProcedureId,procedureId);
        List<ProcedureSequence> procedureSequences = procedureSequenceMapper.selectList(wrapper);
        //转成vo,set集合返回
        List<ProcedureSequenceVO> procedureSequenceVOList = procedureSequences.stream().map(procedureSequence -> {
            ProcedureSequenceVO procedureSequenceVO = new ProcedureSequenceVO();
            BeanUtils.copyProperties(procedureSequence, procedureSequenceVO);
            return procedureSequenceVO;
        }).collect(Collectors.toList());
        procedureVO.setList(procedureSequenceVOList);
        return procedureVO;
    }

    @Override
    public void updateInfoProcedureById(ProcedureVO procedureVO) {
        Procedure procedure =new Procedure();
        BeanUtils.copyProperties(procedureVO,procedure);
        procedureMapper.updateById(procedure);
    }

    @Override
    public void deleteInfoProcedureById(String procedureId) {
        //需要删除两张表的信息
        //工序表的信息
        procedureMapper.deleteById(procedureId);
        //工序顺序表的信息
        LambdaQueryWrapper<ProcedureSequence> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcedureSequence::getProcedureId,procedureId);
        procedureSequenceMapper.delete(wrapper);
    }

    @Override
    public List<Procedure> getInfoListByProcedureIds(List<String> list) {
        List<Procedure> procedures = procedureMapper.selectBatchIds(list);
        return procedures;
    }
}
